<template>
  <!-- 工作组及对应结果 -->
  <div id="wL">
    <Splitpanes>
      <Pane size="100">
        <vxe-toolbar perfect>
          <template #buttons>
            <div class="smallToolTitle inspeResultsTitle margin-right">检验结果</div>
<!--            <vxe-button type="text" icon="vxe-icon-refresh" class="button-blue" content="查询" @click="handleSearch"></vxe-button>-->

            <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="删除" @click="handleDelete"></vxe-button>
            <vxe-button type="text" icon="vxe-icon-add" class="button-blue" content="保存" @click="save"></vxe-button>
          </template>
        </vxe-toolbar>
        <div class="tableRight">
          <!-- <vxe-toolbar perfect>
            <template #buttons>
            </template>
          </vxe-toolbar>-->
          <vxe-table ref="wLResultTable" border align="left" resizable show-overflow height="auto" :scroll-y="{ enabled:true }" v-loading="loading" :data="wLResultList" :row-config="{ isCurrent: true, isHover: true }" :keyboard-config="{ isArrow: true }" :cell-style="calcCellBgColor">
            <!-- <vxe-column type="checkbox" width="30px" align="center"></vxe-column> -->
            <vxe-column field="sampleNo" title="标本号" width="150"></vxe-column>
            <vxe-column field="itemNo" title="检验项目" width="300">
              <template #default="{ row }">{{ `${row.itemNo} ${row.itemNa}` }}</template>
            </vxe-column>
            <vxe-column field="testResult" title="结果" width="150">
              <template #default="{ row }">{{getFloatResult(row.testResult)}}</template>
            </vxe-column>
            <vxe-column field="adjustResult" title="调整后结果" width="150">
              <template #default="{ row }">{{getFloatResult(row.adjustResult)}}</template>
            </vxe-column>
            <vxe-column field="resultFlag" title width="30">
              <template #default="{ row }">
                <span class="padding-lr-xs text-df">{{ getResultFlag(row.resultFlag) }}</span>
                <span class="padding-lr-xs" v-if="row.isAlarm === '1' || row.isAlarm === 1">{{ row.alarmText }}</span>
              </template>
            </vxe-column>

            <vxe-column field="ref" title="参考范围" width="200"></vxe-column>
          </vxe-table>
        </div>
      </Pane>
    </Splitpanes>
  </div>
</template>
<script setup lang="ts">
// 引入组件区 ==========
import { reactive, ref, defineExpose, getCurrentInstance, onMounted } from 'vue';
import { VXETable } from 'vxe-table';
import { IsEmpty, formatDateTime } from '@/utils/common.ts';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { Splitpanes, Pane } from 'splitpanes';
import { ElLoading } from 'element-plus';
import { getOptionValueByCodeGlobalOption, getOptionValueByCodeRptGroupOption } from '@/utils/SysOptionsHelper';

// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
let wLResultList: any = reactive([]); //项目列表数据
let form: any = ref();
let loading = ref(false); //刷新+表格加载
defineExpose({
  getWklt
});

// 方法定义区 =============
function handleSearch() {
  getWklt(form);
}
// 获取工作列表
function getWklt(formWklt) {
  wLResultList.length = 0;
  form = formWklt;
  const url = '/lab/maininfo/findInspectResultList';
  const data = {
    wtId: form.selectWtId,
    rptGroup: form.wtId,
    scheduleDateBegin: IsEmpty(form.scheduleDate) ? '' : formatDateTime(form.scheduleDate, 'yyyy-MM-dd') + ' 00:00:00',
    scheduleDateEnd: IsEmpty(form.scheduleDate) ? '' : formatDateTime(form.scheduleDate, 'yyyy-MM-dd') + ' 23:59:59',
    visitTypeIds: form.visitTypeIds,
    specTypeIds: form.specTypeIds,
    sampleNo: form.sampleNo,
    isReviewed: IsEmpty(form.isReviewed) || form.isReviewed === 0 || form.isReviewed === '0' ? null : form.isReviewed, //是否审核
    isPrinted: IsEmpty(form.isPrinted) || form.isPrinted === 0 || form.isPrinted === '0' ? null : form.isPrinted, //是否打印
    isFilterNoResult: form.isFilterNoResult, //根据结果筛选
    isFilterNoSpecCode: form.isFilterNoSpecCode, //根据条码号筛选
    isFilterNoPatName: form.isFilterNoPatName, //根据病人姓名筛选
    customSorting: form.customSorting, //打印顺序
    itemDictId: form.labItemDictId
  };
  proxy.$http
    .post(url, data)
    .then(async (res: any) => {
      wLResultList.length = 0;
      wLResultList.push(...res.data);
    })
    .catch((err: any) => {
      VxeMessage(err, VxeNotifyType.error);
    });
}

function handleDelete() {
  console.log('00o00900');
}

function JsResult() {
  if (form.needFormulae == '0') {
    wLResultList.forEach((element) => {
      element.adjustResult = element.testResult * form.ratio + form.step;
    });
  } else if (form.needFormulae == '1') {
    wLResultList.forEach((element) => {
      element.adjustResult = eval(form.formulae.replace('R', element.testResult));
    });
  }
}

// 批量保存数据
async function save() {
  const type = await VXETable.modal.confirm(`确定批次调整?`); /*不能改el  $confirm 回车事件冒泡你*/
  if (type !== 'confirm') {
    return;
  }
  JsResult();
  const loadingInstance = ElLoading.service({ lock: true, text: '请稍后...', background: 'rgba(0,0,0,0)' });
  let url = '/lab/maininfo/saveTestResultBatch';
  let data = {
    ratio: form.ratio,
    step: form.step,
    needFormulae: form.needFormulae,
    formulae: form.formulae,
    amendedReason: form.amendedReason,
    InspectResults: wLResultList,
    sampleda: formatDateTime(form.scheduleDate, 'yyyy-MM-dd')
  };
  proxy.$http
    .post(url, data)
    .then(async (res: any) => {
      if (res.status.code === 1) {
        VxeMessage(res.status.msg, VxeNotifyType.success);
        getWklt(form);
      } else {
        VxeMessage(res.status.msg, VxeNotifyType.error);
      }
      loadingInstance.close();
    })
    .catch((err: any) => {
      loadingInstance.close();
      VxeMessage(err, VxeNotifyType.error);
    });
}
function getResultFlag(val) {
  const a = getOptionValueByCodeRptGroupOption('zjmaccsxs');
  if (a === '1' || a === 1) {
    if (val === 'H') {
      const b = getOptionValueByCodeRptGroupOption('pgjgbz');
      if (!IsEmpty(b)) {
        return b;
      }
      return '↑';
    }
    if (val === 'L') {
      const b = getOptionValueByCodeRptGroupOption('pdjgbz');
      if (!IsEmpty(b)) {
        return b;
      }
      return '↓';
    }
    if (val === 'P') {
      const b = getOptionValueByCodeRptGroupOption('yxjgbz');
      if (!IsEmpty(b)) {
        return b;
      }
      return '+';
    }
    if (val === 'Q') {
      const b = getOptionValueByCodeRptGroupOption('ryxjgbz');
      if (!IsEmpty(b)) {
        return b;
      }
      return '±';
    }
    if (val === 'M') {
      const b = getOptionValueByCodeRptGroupOption('zcjgbz');
      if (!IsEmpty(b)) {
        return b;
      }
      return '';
    }
    if (val === 'E') {
      const b = getOptionValueByCodeRptGroupOption('cwjgbz');
      if (!IsEmpty(b)) {
        return b;
      }
      return '×';
    }
    if (val === 'N') {
      const b = getOptionValueByCodeRptGroupOption('nxjgbz');
      if (!IsEmpty(b)) {
        return b;
      }
      return '√';
    }
  }
  if (val === 'H') {
    return '↑';
  }
  if (val === 'L') {
    return '↓';
  }
  if (val === 'P') {
    return '+';
  }
  if (val === 'Q') {
    return '±';
  }
  if (val === 'N') {
    return '√';
  }
  if (val === 'E') {
    return '×';
  }
  if (val === 'M') {
    return '';
  }
  return '';
}
/**
 * 单元格背景颜色
 *【1】是否启用系统选项颜色配置  encolorsetting
 *【2】偏高结果颜色  hresultcolor
 *【3】偏低结果颜色  lresultcolor
 *【4】阳性结果颜色  presultcolor
 *【5】正常结果颜色  mresultcolor
 *【6】弱阳性结果颜包  qresultcolor
 *【7】错误结果颜色  errresultcolor
 *【8】阴性结果颜色  nresultcolor
 *【9】结果标志颜色显示方式 jgbzysxsfs (0:背景色  1:文字颜色)
 */
function calcCellBgColor({ row, rowIndex, $rowIndex, column, columnIndex, $columnIndex }) {
  /**偏高【标志】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'H' && column && column.field === 'resultFlag') {
    //查询系统选项偏高显示颜色   背景色还是文字颜色
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const b = getOptionValueByCodeRptGroupOption('jgbzysxsfs');
      const c = getOptionValueByCodeGlobalOption('hresultcolor');
      if (b === '0' || b === 0) {
        if (!IsEmpty(c)) {
          return { background: c, color: '#fff' };
        }
      }
      if (b === '1' || b === 1) {
        if (!IsEmpty(c)) {
          return { color: c };
        }
      }
    }
    return { color: '#ff0000' };
  }
  /**偏高【结果】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'H' && column && column.field === 'testResult') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const c = getOptionValueByCodeGlobalOption('hresultcolor');
      if (!IsEmpty(c)) {
        return { background: c, color: '#fff' };
      }
    }
    return { background: '#ff0000', color: '#fff' };
  }

  /**偏低【标志】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'L' && column && column.field === 'resultFlag') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const b = getOptionValueByCodeRptGroupOption('jgbzysxsfs');
      const c = getOptionValueByCodeGlobalOption('lresultcolor');
      if (b === '0' || b === 0) {
        if (!IsEmpty(c)) {
          return { background: c, color: '#fff' };
        }
      }
      if (b === '1' || b === 1) {
        if (!IsEmpty(c)) {
          return { color: c };
        }
      }
    }
    return { color: '#0035ff' };
  }
  /**偏低【结果】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'L' && column && column.field === 'testResult') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const c = getOptionValueByCodeGlobalOption('lresultcolor');
      if (!IsEmpty(c)) {
        return { background: c, color: '#fff' };
      }
    }
    return { background: '#0035ff', color: '#fff' };
  }

  /**阳性【标志】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'P' && column && column.field === 'resultFlag') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const b = getOptionValueByCodeRptGroupOption('jgbzysxsfs');
      const c = getOptionValueByCodeGlobalOption('presultcolor');
      if (b === '0' || b === 0) {
        if (!IsEmpty(c)) {
          return { background: c, color: '#fff' };
        }
      }
      if (b === '1' || b === 1) {
        if (!IsEmpty(c)) {
          return { color: c };
        }
      }
    }
    return { color: '#0035ff' };
  }
  /**阳性【结果】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'P' && column && column.field === 'testResult') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const c = getOptionValueByCodeGlobalOption('presultcolor');
      if (!IsEmpty(c)) {
        return { background: c, color: '#fff' };
      }
    }
    return { background: '#0035ff', color: '#fff' };
  }

  /**弱阳性【标志】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'Q' && column && column.field === 'resultFlag') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const b = getOptionValueByCodeRptGroupOption('jgbzysxsfs');
      const c = getOptionValueByCodeGlobalOption('qresultcolor');
      if (b === '0' || b === 0) {
        if (!IsEmpty(c)) {
          return { background: c, color: '#fff' };
        }
      }
      if (b === '1' || b === 1) {
        if (!IsEmpty(c)) {
          return { color: c };
        }
      }
    }
    return { color: '#0035ee' };
  }
  /**弱阳性【结果】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'Q' && column && column.field === 'testResult') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const c = getOptionValueByCodeGlobalOption('qresultcolor');
      if (!IsEmpty(c)) {
        return { background: c, color: '#fff' };
      }
    }
    return { background: '#0035ee', color: '#fff' };
  }

  /**错误【标志】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'E' && column && column.field === 'resultFlag') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const b = getOptionValueByCodeRptGroupOption('jgbzysxsfs');
      const c = getOptionValueByCodeGlobalOption('errresultcolor');
      if (b === '0' || b === 0) {
        if (!IsEmpty(c)) {
          return { background: c, color: '#fff' };
        }
      }
      if (b === '1' || b === 1) {
        if (!IsEmpty(c)) {
          return { color: c };
        }
      }
    }
    return { color: '#800000' };
  }
  /**错误【结果】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'E' && column && column.field === 'testResult') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const c = getOptionValueByCodeGlobalOption('errresultcolor');
      if (!IsEmpty(c)) {
        return { background: c, color: '#fff' };
      }
    }
    return { background: '#800000', color: '#fff' };
  }

  /**正常【标志】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'M' && column && column.field === 'resultFlag') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const b = getOptionValueByCodeRptGroupOption('jgbzysxsfs');
      const c = getOptionValueByCodeGlobalOption('mresultcolor');
      if (b === '0' || b === 0) {
        if (!IsEmpty(c)) {
          return { background: c, color: '#fff' };
        }
      }
      if (b === '1' || b === 1) {
        if (!IsEmpty(c)) {
          return { color: c };
        }
      }
    }
    return { color: '#000' };
  }
  /**正常【结果】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'M' && column && column.field === 'testResult') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const c = getOptionValueByCodeGlobalOption('mresultcolor');
      if (!IsEmpty(c)) {
        return { background: c, color: '#fff' };
      }
    }
    return { color: '#000' };
  }

  /**阴性【标志】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'N' && column && column.field === 'resultFlag') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const b = getOptionValueByCodeRptGroupOption('jgbzysxsfs');
      const c = getOptionValueByCodeGlobalOption('nresultcolor');
      if (b === '0' || b === 0) {
        if (!IsEmpty(c)) {
          return { background: c, color: '#fff' };
        }
      }
      if (b === '1' || b === 1) {
        if (!IsEmpty(c)) {
          return { color: c };
        }
      }
    }
    return { color: '#000' };
  }
  /**阴性【结果】 */
  if (row && row.resultFlag && !IsEmpty(row.resultFlag) && row.resultFlag === 'N' && column && column.field === 'testResult') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    if (a === '1' || a === 1) {
      const c = getOptionValueByCodeGlobalOption('nresultcolor');
      if (!IsEmpty(c)) {
        return { background: c, color: '#fff' };
      }
    }
    return { color: '#000' };
  }

  /**危急值 颜色 */
  if (row && !IsEmpty(row.isAlarm) && row.isAlarm === 1 && column && column.field === 'itemNo') {
    const a = getOptionValueByCodeGlobalOption('encolorsetting');
    const b = getOptionValueByCodeGlobalOption('isalarmcolor');
    const d = getOptionValueByCodeGlobalOption('wjzyssfxswbjys'); /**危急值颜色显示是否为背景颜色 wjzyssfxswbjys */
    if (a === 1 || a === '1') {
      if (!IsEmpty(b)) {
        if (d && (d === '1' || d === 1)) {
          return { background: b, color: '#fff' };
        } else {
          return { color: b };
        }
      }
    }
  }
}
function getFloatResult(result) {
  if (result) {
    return parseFloat(result).toFixed(2);
  } else {
    return result;
  }
}
</script>
<style lang="scss" scoped>
#wL {
  height: 100%;
  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }
  .tableBody {
    height: calc(100% - 40px);
  }
  .inspeResultsTitle {
    height: 34px;
    line-height: 34px;
  }
  .tableRight {
    height: calc(100% - 40px);
  }
}
:deep(.flag-cell) {
  .vxe-cell {
    padding: 0 2px !important;
  }
}
</style>
